package org.jf.tool.fx.jftoolfx;

import atlantafx.base.theme.*;
import javafx.application.Application;
import javafx.collections.ObservableList;
import javafx.fxml.FXMLLoader;
import javafx.geometry.Rectangle2D;
import javafx.scene.Scene;
import javafx.stage.Screen;
import javafx.stage.Stage;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.jf.tool.fx.jftoolfx.util.SettingsUtil;

import java.io.IOException;
import java.security.Security;

public class Main extends Application {
    /**
     * 顶级 JavaFX 容器
     */
    protected static Stage TOP_STAGE;


    static {
        // 注册 Bouncy Castle
        Security.addProvider(new BouncyCastleProvider());
    }

    @Override
    public void start(Stage stage) throws IOException {
        TOP_STAGE = stage;
        String theme = SettingsUtil.getTheme();
        switch (theme) {
            case "PrimerLight":
                Application.setUserAgentStylesheet(new PrimerLight().getUserAgentStylesheet());
                break;
            case "PrimerDark":
                Application.setUserAgentStylesheet(new PrimerDark().getUserAgentStylesheet());
                break;
            case "CupertinoLight":
                Application.setUserAgentStylesheet(new CupertinoLight().getUserAgentStylesheet());
                break;
            case "CupertinoDark":
                Application.setUserAgentStylesheet(new CupertinoDark().getUserAgentStylesheet());
                break;
            case "Dracula":
                Application.setUserAgentStylesheet(new Dracula().getUserAgentStylesheet());
                break;
            case "NordLight":
                Application.setUserAgentStylesheet(new NordLight().getUserAgentStylesheet());
                break;
            case "NordDark":
                Application.setUserAgentStylesheet(new NordDark().getUserAgentStylesheet());
                break;
            default:
                Application.setUserAgentStylesheet(new PrimerLight().getUserAgentStylesheet());
        }
        FXMLLoader fxmlLoader = new FXMLLoader(Main.class.getResource("main.fxml"));
        Scene scene = new Scene(fxmlLoader.load(), 833, 611);
        // 获取所有屏幕-解决多屏模式下窗口不居中显示的问题
        ObservableList<Screen> screens = Screen.getScreens();
        if (screens.size() > 1) {
            Screen secondaryScreen = screens.get(1);
            Rectangle2D bounds = secondaryScreen.getVisualBounds();
            stage.setX(bounds.getMinX() + (bounds.getWidth() - stage.getWidth()) / 2);
            stage.setY(bounds.getMinY() + (bounds.getHeight() - stage.getHeight()) / 2);
        }
        stage.setTitle("JF-工具");
        stage.setScene(scene);
        scene.getStylesheets().add(getClass().getResource("/static/css/style.css").toExternalForm());
        // 窗口居中
        stage.centerOnScreen();
        stage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }

}